﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TLT.Models.Entities;
using TLT.Models.Views;
using TLT.Models.UOW;
using TLT.Models.Repositories;
using System.Web.Http.Description;
using System.Data.Entity.Infrastructure;
using AutoMapper;
using System.Web.Http.ModelBinding;

namespace shopruvi.Areas.Admin.Controllers
{
    public class NhomNguoiDungController : BaseController
    {
        private UOW context = new UOW();

        public IEnumerable<NhomNguoiDung> GetAll()
        {
            return context.NhomNguoiDungRepository.GetAll();
        }

        [ResponseType(typeof(NhomNguoiDung))]
        public IHttpActionResult GetObjectByID(Guid id)
        {
            var obj = context.NhomNguoiDungRepository.GetObjectByID(id);
            if (obj == null)
            {
                return Ok(new ErrorView(StatusMess.LoiKhongTimThayDoiTuong));
            }
            return Ok(obj);
        }

        [ResponseType(typeof(NhomNguoiDung))]
        public IHttpActionResult Save(NhomNguoiDungView obj)
        {
            Validation(obj, ModelState);

            if (!ModelState.IsValid)
            {
                return Ok(new ErrorView(StatusMess.NotAvailableMess){Errors = ErrorList(ModelState) });
            }

            try
            {
                var objEntity = new NhomNguoiDung();
                Mapper.Initialize(p => p.CreateMap<NhomNguoiDungView, NhomNguoiDung>());
                objEntity = Mapper.Map<NhomNguoiDungView, NhomNguoiDung>(obj);

                if (!context.NhomNguoiDungRepository.ObjectExists(obj.Id))
                {
                    if (obj.Id==Guid.Empty)
                    {
                        context.NhomNguoiDungRepository.Add(objEntity);
                        context.Save();
                        return Ok(objEntity);
                    }

                    return Ok(new ErrorView(StatusMess.LoiKhongTimThayDoiTuong));
                }
                else
                {
                    context.NhomNguoiDungRepository.Update(objEntity);
                    context.Save();
                    return Ok(objEntity);
                }
            }
            catch (Exception)
            {
                return Ok(new ErrorView(StatusMess.LoiTrongQuaTrinhLuu));
            }
        }

        [HttpPost]
        public IHttpActionResult Delete(Guid id)
        {
            try
            {
                if (!context.NhomNguoiDungRepository.ObjectExists(id))
                {
                    return Ok(new ErrorView(StatusMess.LoiKhongTimThayDoiTuong));
                }
                context.NhomNguoiDungRepository.Delete(id);
                context.Save();
                return Ok(id);
            }
            catch (Exception)
            {
                return Ok(new ErrorView(StatusMess.LoiTrongQuaTrinhXoa));
            }
        }

        public IHttpActionResult CheckDuplicateCode(Guid id, string code)
        {
            try
            {
                if (context.NhomNguoiDungRepository.CheckDuplicateCode(id,code))
                {
                    return Ok(new BoolView { BoolResult = true });
                }
                else
                {
                    return Ok(new BoolView { BoolResult = false });
                }
            }
            catch (Exception)
            {

                return Ok(new BoolView { BoolResult = false });
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                context.Dispose();
            }
            base.Dispose(disposing);
        }

        public ModelStateDictionary Validation(NhomNguoiDungView obj, ModelStateDictionary modelState)
        {
            RemoveError(modelState);

            if (string.IsNullOrWhiteSpace(obj.Ma))
            {
                modelState.AddModelError("", "Chưa nhập mã.");
            }

            if (context.NhomNguoiDungRepository.CheckDuplicateCode(obj.Id, obj.Ma))
            {
                modelState.AddModelError("", "Mã đã tồn tại, nhập mã khác.");
            }

            if (string.IsNullOrWhiteSpace(obj.Ten))
            {
                modelState.AddModelError("", "Chưa nhập tên.");
            }

            return modelState;
        }
    }
}
